package me.pplay.playtalk.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.setting.SettingActivity;
import me.pplay.playtalk.adapter.CommonAdapter;
import me.pplay.playtalk.bean.CommonControlID;
import me.pplay.playtalk.callback.CommonAdapterOnClickCallBack;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.protocol.DataCallBack;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.TpChatRoom;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import me.pplay.playtalk.protocol.bean.TpGroup;
import me.pplay.playtalk.service.PlayTalkService;
import me.pplay.playtalk.util.LogManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

public class ChatViewMemberActivity extends BaseActivity {
	private List<Map<String, Object>> mData;
	private ListView list;
	private TpChat tpChat;
	private TpChatRoom tpChatRoom;
	private LinkedList<TPBuddyinfo> friendlist;
	private List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
	private ProgressDialog progressDialog;
	private CommonAdapter adapter;
	private ImageButton btn_back;
	private ImageButton btn_invite;
	public static final int COMMUNITY_TYPE = 1;
	public static final int P2P_TYPE = 2;
	public static final int GROUP_TYPE = 3;
	
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case TpChat.CHAT_GETINFO_OK:
				progressDialog.dismiss();
				Intent intent = new Intent();
				intent.putExtra("skyid", ((TPBuddyinfo)msg.obj).getSkyid());
				intent.setClass(ChatViewMemberActivity.this, ContactDetailsAboutActivity.class);
				startActivity(intent);
				break;
			case TpChat.CHAT_GETINFO_ERR:
				progressDialog.dismiss();
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Unkown err" : rspdata.getString());
				break;
			case TpChat.CHAT_GETINFO_TIMEOUT:
				progressDialog.dismiss();
				showToast("Time out");
				break;
			default:
				break;
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_viewmember);
		super.SetOnBackClickListener((ImageButton)findViewById(R.id.viewmember_btnback));
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		tpChat = appState.getTpChat();
		tpChatRoom = appState.getTpChatRoom();

		Bundle bundle = getIntent().getExtras();
		if(COMMUNITY_TYPE == bundle.getInt("type"))
			friendlist = tpChatRoom.tp_chatroom_get_buddieslist();
		else if(P2P_TYPE == bundle.getInt("type")){
			friendlist = new LinkedList<TPBuddyinfo>();
			TPBuddyinfo tpBuddyinfo = (TPBuddyinfo) bundle.getSerializable("member");
			friendlist.add(Globals.myInfo);
			friendlist.add(tpBuddyinfo);
		}
		else if(GROUP_TYPE == bundle.getInt("type")){
			friendlist = tpChat.tp_chat_getConferenceBuddiesList(bundle.getInt("id"));
		}
		mData = getData();

		CommonControlID holderId = new CommonControlID();
		holderId.itemLayout = R.layout.layout_viewmemberlist;
		holderId.leftImgId = R.id.viewmemberlist_avatar;
		holderId.rightImgId = R.id.viewmemberlist_view;
		holderId.leftStrId = R.id.viewmemberlist_alias;
		adapter = new CommonAdapter(ChatViewMemberActivity.this, mData, holderId, new CommonAdapterOnClickCallBack() {

			@Override
			public void OnClickCallback(int pos, int ViewId, View v) {
				// TODO Auto-generated method stub
				LogManager.LogShow("ChatViewMemberActivity", "OnClickCallback position " + pos);

			}
		});
		list = (ListView) this.findViewById(R.id.viewmember_listView); // 联系人ListView
		list.setAdapter(adapter);// 将数据适配器与Activity进行绑定
		btn_back	= (ImageButton)findViewById(R.id.viewmember_btnback);
		btn_invite = (ImageButton)findViewById(R.id.viewmember_btninvite);
		clickEvents();
	}

	private List<Map<String, Object>> getData() {
		listData.clear();
		if (friendlist != null) {
			for (TPBuddyinfo f:friendlist) {
				LogManager.LogShow("ChatViewMemberActivity", "f.getAlias() " + f.getAlias());
				Map<String, Object> map = new HashMap<String, Object>();
				if (f.getAlias() != null && f.getAlias().length() > 0)
					map.put("leftStr", f.getAlias());
				else if(f.getName() != null && f.getName().length() > 0)
					map.put("leftStr", f.getName());
				else
					map.put("leftStr", Integer.toString(f.getSkyid()));
				map.put("leftImg", R.drawable.friends_default);
				map.put("rightImg", R.drawable.commnutiy_chat);
				listData.add(map);
			}
		}
		return listData;
	}

	private void clickEvents() {
		btn_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ChatViewMemberActivity.this.finish();
			}
		});
		btn_invite.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent2 = new Intent(ChatViewMemberActivity.this, ChatConversationActivity.class);
				intent2.putExtra("from", "newConversation");
				startActivity(intent2);
			}
		});
		
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				LogManager.LogShow("ChatViewMemberActivity", "tp_chat_get_info ");
				final TPBuddyinfo tpBuddyinfo = friendlist.get(arg2);
				if (tpBuddyinfo == null)
					return;
				if(tpBuddyinfo.getSkyid() == Globals.skyid){
					Intent intent = new Intent();
					intent.putExtra("skyid", Globals.skyid);
					intent.setClass(ChatViewMemberActivity.this, ContactDetailsAboutActivity.class);
					startActivity(intent);
				}else{
					progressDialog = ProgressDialog.show(ChatViewMemberActivity.this, getString(R.string.loading), getString(R.string.waittng), true, false); 
					progressDialog.show();
					tpChat.tp_chat_get_info(tpBuddyinfo.getSkyid(), new DataCallBack() {
						 
						@Override
						public void Callback(int result, RspData resultdata, Object obj) {
							LogManager.LogShow("ChatViewMemberActivity", "tp_chat_get_info result:" + result); 
							// TODO Auto-generated method stub
							Message msg = new Message(); 
							msg.what = result; 
							msg.obj = resultdata;
							if (TpChat.CHAT_GETINFO_OK == result) {
								msg.obj = tpBuddyinfo;
							} else if (TpChat.CHAT_GETINFO_ERR == result) {
							} else if (TpChat.CHAT_GETINFO_TIMEOUT == result) {
							} 
							handler.sendMessage(msg);
						}
					});
				}
			}
		});

	}

	@Override
	protected void onStart() {
		PlayTalkService.getInstance().setHandler(handler);
		super.onStart();
	}

	@Override
	protected void onStop() {
		PlayTalkService.getInstance().setHandler(null);
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		friendlist = null;
	}
}
